package com.dxq.basic_ui_library;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * @author zsh
 * @date 2020/2/17.
 * 功能：自定义输入框，带删除按钮
 */

public class InputView extends FrameLayout {

    private View mView;
    private LinearLayout llView;
    private EditText mEtInput;
    private ImageView mImageView;
    private int inputBg;
    private int height;
    private String inputHint;
    private int inputSize;
    private int inputColor;
    private int inputHintColor;
    private int clearIcon;

    public InputView(Context context) {
        super(context);
        init(context, null);
    }

    public InputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public InputView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public InputView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }

    //初始化
    private void init (Context context, AttributeSet attrs) {
        if (attrs == null) return;
        //获取自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.InputView);
        //输入框背景
        inputBg = typedArray.getResourceId(R.styleable.InputView_input_background, R.drawable.input_bg);
        //输入框高度
        height = typedArray.getDimensionPixelOffset(R.styleable.InputView_height, 40);
        //提示内容
        inputHint = typedArray.getString(R.styleable.InputView_input_hint);
        //字体大小
        inputSize  = typedArray.getDimensionPixelSize(R.styleable.InputView_input_size, 20);
        //字体颜色
        inputColor = typedArray.getColor(R.styleable.InputView_input_color, Color.BLACK);
        //提示字体颜色
        inputHintColor = typedArray.getColor(R.styleable.InputView_input_hint_color, Color.GRAY);
        //清空按钮
        clearIcon = typedArray.getResourceId(R.styleable.InputView_image_icon, R.drawable.clear);

        typedArray.recycle();

        mView = LayoutInflater.from(context).inflate(R.layout.input_view, this, false);
        llView = (LinearLayout) mView.findViewById(R.id.ll_view);
        mEtInput = (EditText) mView.findViewById(R.id.et_input);
        mImageView = (ImageView) mView.findViewById(R.id.iv_icon);
        addView(mView);

        //布局关联属性
        llView.setBackgroundResource(inputBg);
        llView.setMinimumHeight(height);
        mEtInput.setHint(inputHint);
        mEtInput.setTextSize(TypedValue.COMPLEX_UNIT_PX, inputSize);
        mEtInput.setTextColor(inputColor);
        mEtInput.setHintTextColor(inputHintColor);
        mImageView.setImageResource(clearIcon);


        if (getInputContent().equals("")) {
            mImageView.setVisibility(GONE);
        }

        mEtInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().equals("")) {
                    mImageView.setVisibility(GONE);
                } else {
                    mImageView.setVisibility(VISIBLE);
                }
            }
        });

        mImageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //mClearContent.clear();
                mEtInput.setText("");
            }
        });

    }

    /**
     * 获取输入框输入内容
     * @return
     */
    public String getInputContent() {
        return mEtInput.getText().toString().trim();
    }
}
